<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="hAdd(1,'0')">添加权限</el-button>
        </div>
        <el-table border :data="list" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{row}">
              <el-button v-if="row.type ===1" type="text" @click="hAdd(2,row.id)">添加</el-button>
              <el-button type="text" @click="isedit(row.id)">编辑</el-button>
              <el-button type="text" @click="doclick(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <!-- 弹层 -->
      <el-dialog title="添加或编辑" :visible.sync="dialogFormVisible" @close="hclose">
        <el-form ref="refu" :model="formData">
          <el-form-item label="权限名称">
            <el-input v-model="formData.name" autocomplete="off" />
          </el-form-item>
          <el-form-item label="权限标识">
            <el-input v-model="formData.code" autocomplete="off" />
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="formData.description" />
          </el-form-item>
          <el-form-item label="权限启用">
            <el-switch
              v-model="formData.enVisible"
              active-text="启用"
              active-value="1"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>
        <template>
          <div slot="footer" class="dialog-footer">
            <el-button @click="cdada">取 消</el-button>
            <el-button type="primary" @click="hsubmit">确 定</el-button>
          </div>
        </template>

      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getPermissionList, delPermission, addPermission, updatePermission, getPermissionDetail } from '@/api/permissions'
import { tranList } from '@/utils'
export default {
  data() {
    return {
      // 树形
      list: [],
      //   弹层开关
      dialogFormVisible: false,
      //   数据
      formData: {
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        enVisible: '0', // 开启
        pid: '', // 添加到哪个节点下
        type: '' // 类型
      },
      isEdit: false
    }
  },
  created() {
    this.loadPermissionList()
  },
  methods: {
    // 实现添加权限功能
    hAdd(type, pid) {
      this.isEdit = false
      this.dialogFormVisible = true
      this.formData.type = type
      this.formData.pid = pid
    },
    // 全部权限信息
    async loadPermissionList() {
      try {
        const res = await getPermissionList()
        console.log(res)
        this.list = tranList(res.data)
      } catch (error) {
        console.log(error)
      }
    },
    // 删除
    async deletelPermission(id) {
      try {
        await delPermission(id)

        this.loadPermissionList()
      } catch (error) {
        console.log(error)
      }
    },
    // 确认删除
    doclick(id) {
      this.$confirm('是否删除', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deletelPermission(id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 新增
    async loadaddPermission() {
      try {
        await addPermission(this.formData)
        this.$message.success('添加权限成功')
        this.loadPermissionList()
      } catch (error) {
        console.log(error)
      }
    },
    // 编辑按钮  获取并填充内容
    async isedit(id) {
      this.isEdit = true
      try {
        this.dialogFormVisible = true
        const res = await getPermissionDetail(id)
        this.formData = res.data
        console.log(res, '编辑信息')
      } catch (error) {
        console.log(error)
      }
    },
    //  确定修改的ajax
    async loadatePermission() {
      try {
        await updatePermission(this.formData)
        this.loadPermissionList()
      } catch (error) {
        console.log(error)
      }
    },
    // 确定新增
    hsubmit() {
      this.dialogFormVisible = false
      this.isEdit ? this.loadatePermission() : this.loadaddPermission()
    },
    // 取消
    cdada() {
      this.dialogFormVisible = false
      this.formData = {}
    },
    // close事件
    hclose() {
      this.$refs.refu.resetFields()
      this.formData = {}
    }

  }
}

</script>
